package artid.com.arttogo.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.WebViewActivity;
import artid.com.arttogo.adapter.IbeconAdapter;
import artid.com.arttogo.bean.IbeaconListParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUrl;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * 
*    
* 项目名称：ArtworkArt   
* 类名称：Ambitus_Fragment   
* 类描述：周边-消息   
* 创建人：耿卫斌   
* 创建时间：2016-6-12 下午1:51:41   
*
 */
public class Ambitus_Fragment extends BaseFragment{
	
	private int page = 1;
	private View v;
	private Context mContext;
	private PullableListView mListView;
	private boolean isRefreshed = false;// 是否刷新
	private List<IbeaconListParser> list;
	private List<IbeaconListParser> more_list;
	private PullToRefreshLayout mPullLayout;
	private IbeconAdapter adapter;
	private View noDataLL;
	private int OnItemPosition = -1;;// 点击的I特么下标
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
							 @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		v=inflater.inflate(R.layout.fragment_ambitus, null);
		initView();
		return v;
	}

	private void initView() {
		mContext = getActivity();
		list = new ArrayList<IbeaconListParser>();
		more_list = new ArrayList<IbeaconListParser>();
		noDataLL = (View) v.findViewById(R.id.sysmess_blank);
		mListView = (PullableListView) v
				.findViewById(R.id.SystemMessage_listivew);
		mPullLayout = (PullToRefreshLayout) v
				.findViewById(R.id.SystemMessage_refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				page = 1;
				isRefreshed = true;
				getSystemMessage();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				page++;
				getSystemMessage();
			}
		});
	}
	
	private void getData() {
		// 注册当前广播
		
		page = 1;
		isRefreshed = true;
		getSystemMessage();
	}
	
	/**
	 * 获取通知列表
	 */
	private void getSystemMessage() {
		page = 1;
		isRefreshed = true;
		VolleyBasic.getInstance(getActivity()).post(Cache.ibeaconList,
				VolleyMap.getInstance().messageList(page, 10),
				IbeaconListParser.class, new VolleyCallback() {
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						System.out.println(vd.getJson());
						mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
						list = (List<IbeaconListParser>) vd.getList();
						if (isRefreshed == true) {
							more_list.clear();
							isRefreshed = false;
						}
						if (!list.isEmpty()) {
							more_list.addAll(list);
						}
						// if(more_list!=null && more_list.size()!=0){
						// noDataLL.setVisibility(View.GONE);
						// }else{
						// noDataLL.setVisibility(View.VISIBLE);
						// return;
						// }
						if (page == 1 && list.isEmpty()) {
							noDataLL.setVisibility(View.VISIBLE);
						} else {
							noDataLL.setVisibility(View.GONE);
						}
						if (adapter == null) {
							adapter = new IbeconAdapter(mContext,
									more_list);
							mListView.setAdapter(adapter);
							mListView.setOnItemClickListener(OnItemClick);
						}
						adapter.notifyDataSetChanged();
					}

					public void onError(VolleyError volleyError) {
						mPullLayout.loadmoreFinish(mPullLayout.FAIL);
					}
				});
	}
//	
	OnItemClickListener OnItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
								long arg3) {
			// TODO Auto-generated method stub
	
			Intent intent = new Intent(mContext,WebViewActivity.class);
			intent.putExtra("type", AppUrl.LOAD_HOMEPAGE_WEB);
			intent.putExtra("url", more_list.get(position).getMessactivityurl());
			startActivity(intent);
		}

	};

	/**
	 * 每次切换Fragment刷新页面
	 */
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		// TODO Auto-generated method stub
		super.setUserVisibleHint(isVisibleToUser);
		// 每次切换fragment时调用的方法
		if (isVisibleToUser) {
			
		}
	}

	public void onResume() {
		super.onResume();
		getData();
		
	};
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
		adapter = null;
		OnItemPosition = -1;
	
	}
	
}
